#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        int cnt = 5;
        while(cnt)
        {
             printf("我是子进程：%d，父进程：%d，cnt=%d\n",getpid(),getppid(),cnt--);
             sleep(1);
            // int* p = NULL;
            // *p = 10;
        }
        exit(10);//进程退出
        //进程退出有3种情况：
        //1、运行完，结果正确
        //2、运行完，结果错误
        //3、没有运行完，出异常了
    }
    //父进程
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0)
    {
        if(WIFEXITED(status))
        {
            printf("child exit code: %d\n",WEXITSTATUS(status));
        }

       // printf("wait success: %d, sig number: %d, child exit code: %d\n",ret, (status & 0x7F), ((status >> 8) & 0xFF));
    }

    return 0;
}
